<?php
/**
 * template_lite youtube modifier plugin
 *
 * Type:     modifier
 * Name:     youtube
 * Purpose:  Converts an element into an embedded Youtube video(s)
 */
function tpl_modifier_youtube($string)
{
    // http://stackoverflow.com/questions/5830387/how-to-find-all-youtube-video-ids-in-a-string-using-a-regex?answertab=active
    // Linkify youtube URLs which are not already links.
    $string = preg_replace('~
        # Match non-linked youtube URL in the wild. (Rev:20111012)
        https?://         # Required scheme. Either http or https.
        (?:[0-9A-Z-]+\.)? # Optional subdomain.
        (?:               # Group host alternatives.
          youtu\.be/      # Either youtu.be,
        | youtube\.com    # or youtube.com followed by
          \S*             # Allow anything up to VIDEO_ID,
          [^\w\-\s]       # but char before ID is non-ID char.
        )                 # End host alternatives.
        ([\w\-]{11})      # $1: VIDEO_ID is exactly 11 chars.
        (?=[^\w\-]|$)     # Assert next char is non-ID or EOS.
        (?!               # Assert URL is not pre-linked.
          [?=&+%\w]*      # Allow URL (query) remainder.
          (?:             # Group pre-linked alternatives.
            [\'"][^<>]*>  # Either inside a start tag,
          | </a>          # or inside <a> element text contents.
          )               # End recognized pre-linked alts.
        )                 # End negative lookahead assertion.
        [?=&+%\w-]*        # Consume any URL (query) remainder.
        ~ix', 
        '
        <link rel="image_src" href="http://img.youtube.com/vi/$1/0.jpg" />
        <div class="modifier_youtube">
        	<object width="728" height="410">
        		<param name="movie" value="//www.youtube.com/v/$1?version=3&amp;showinfo=0&amp;hl=en_GB&amp;rel=0;nologo=1"></param>
        		<param name="allowFullScreen" value="true"></param>
        		<param name="allowscriptaccess" value="always"></param>
        		<embed src="//www.youtube.com/v/$1?version=3&amp;hl=en_GB&amp;showinfo=0&amp;rel=0;nologo=1" type="application/x-shockwave-flash" width="728" height="410" allowscriptaccess="always" allowfullscreen="true"></embed>
        	</object>
        </div>',
        $string);
    return $string;
}

?>